package com.ruoyi.userSystem.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.userSystem.domain.Role;
import com.ruoyi.userSystem.domain.User_Role;
import com.ruoyi.userSystem.service.IRoleService;
import com.ruoyi.userSystem.service.IUser_RoleServices;
import com.ruoyi.utils.MD5Util;
import com.ruoyi.utils.UUIDUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.userSystem.domain.User;
import com.ruoyi.userSystem.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 * 
 * @author zzzyh
 * @date 2025-06-10
 */
@RestController
@RequestMapping("/userSystem/userSystem")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUser_RoleServices user_roleServices;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('userSystem:userSystem:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('userSystem:userSystem:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('userSystem:userSystem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('userSystem:userSystem:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        System.out.println("user:" + user);
        user.setSalt(UUIDUtil.getUUID_32());
        user.setPassword("{noop}"+MD5Util.md5BySalt(user.getPassword(),user.getSalt()));
        Role role;
        if(user.getIsAdmin() == 0)
            role = roleService.selectByName("user");
        else if (user.getIsAdmin() == 1) {
            role = roleService.selectByName("admin");
        }else{
            role =  roleService.selectByName("user");
        }
        user.setRole(role);
        int ret = userService.insertUser(user);
        User_Role userRole = new User_Role();
        userRole.setUserId(user.getId());
        userRole.setRoleId(role.getId());
        user_roleServices.createUser_Role(userRole);
        return toAjax(ret);
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('userSystem:userSystem:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        user.setSalt(UUIDUtil.getUUID_32());
        user.setPassword("{noop}"+MD5Util.md5BySalt(user.getPassword(),user.getSalt()));
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('userSystem:userSystem:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }


    @GetMapping("/checkUserSystemNameUnique/{username}")
    public Boolean checkUserSystemNameUnique(@PathVariable String username)
    {
        Long userId = userService.getUserIdByUsername(username);
        if (userId != null)
        {
            return true;
        }
        return false;
    }
}
